package com.example.springbootstartermqttprovider.quert;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExpUtils {

    public static boolean validateMobile(String mobile) {
        String regex = "^((13[0-9])|(14[5-9])|(15([0-3]|[5-9]))|(16[2,5-7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$";
        return Pattern.matches(regex, mobile);
    }

    public static boolean validatePassword(String password) {
        String regex = "(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\\W_]).{8,16}";
        return Pattern.matches(regex, password);
    }

    public static boolean validateBikeNo(String bikeNo) {
        String regex = "^[0-9a-zA-Z]{5,12}$";
        return Pattern.matches(regex, bikeNo);
    }

    public static boolean validateBikeType(String bikeNo, String regex) {
        return Pattern.matches(regex, bikeNo);
    }

    public static boolean validateMacAddr(String macAddr) {
        String regex = "[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}";
        return Pattern.matches(regex, macAddr);
    }

    public static List<String> grep(String content, String regex) {
        List<String> ms = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            ms.add(matcher.group(0));
        }
        return ms;
    }

    public static List<String> grepTwo(String content, String regex) {
        List<String> ms = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            ms.add(matcher.group(1));
        }
        return ms;
    }


    public static boolean isValidLatitudeLongitude(String str) {
        if (str == null || str.isEmpty()) {
            return false;
        }

        // 以逗号分隔字符串
        String[] parts = str.split(",");
        if (parts.length != 2) {
            return false;
        }

        try {
            // 解析经纬度
            double longitude = Double.parseDouble(parts[0].trim());
            double latitude = Double.parseDouble(parts[1].trim());

            // 验证经度和纬度是否在有效范围内
            return isValidLatitude(latitude) && isValidLongitude(longitude);
        } catch (NumberFormatException e) {
            // 如果解析失败，返回false
            return false;
        }
    }
    // 验证纬度是否在 -90 到 90 之间
    private static boolean isValidLatitude(double latitude) {
        return latitude >= -90.0 && latitude <= 90.0;
    }

    // 验证经度是否在 -180 到 180 之间
    private static boolean isValidLongitude(double longitude) {
        return longitude >= -180.0 && longitude <= 180.0;
    }

}
